﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Moonlite.UI.Shell.Controls {
    public class ProjectListBox : UserControl {
        private const int MaxListLength = 6;
        private const int BorderRadius = 3;
        public int SelectedIndex { get; set; }
        public event SelectedIndexChangedEventHandler SelectedIndexChanged;
        public int ItemHeight {
            get { return (int)(Font.Height * 1.25); }
        }

        private StartPageRecentProjectCollection items = new StartPageRecentProjectCollection();
        public StartPageRecentProjectCollection Items {
            get { return items; }
            set { items = value; }
        }

        private static GraphicsPath CreateRoundedRectangle(int x, int y, int width, int height, int radius) {
            int xw = x + width;
            int yh = y + height;
            int xwr = xw - radius;
            int yhr = yh - radius;
            int xr = x + radius;
            int yr = y + radius;
            int r2 = radius * 2;
            int xwr2 = xw - r2;
            int yhr2 = yh - r2;
            GraphicsPath path = new GraphicsPath();
            path.StartFigure();
            path.AddArc(x, y, r2, r2, 180, 90);
            path.AddLine(xr, y, xwr, y);
            path.AddArc(xwr2, y, r2, r2, 270, 90);
            path.AddLine(xw, yr, xw, yhr);
            path.AddArc(xwr2, yhr2, r2, r2, 0, 90);
            path.AddLine(xwr, yh, xr, yh);
            path.AddArc(x, yhr2, r2, r2, 90, 90);
            path.AddLine(x, yhr, x, yr);
            path.CloseFigure();
            return path;
        }

        protected override void OnPaint(PaintEventArgs pe) {
            float yPos = 1;
            float itemHeight = ItemHeight;
            int curItem = 0;
            Graphics graphics = pe.Graphics;
            SelectedIndex = -1;
            Cursor = Cursors.Default;
            while (curItem < Math.Min(MaxListLength, Items.Count) && yPos < Height) {
                RectangleF drawingBackground = new RectangleF(1, yPos, Width - 2, itemHeight);
                if (drawingBackground.IntersectsWith(pe.ClipRectangle) == true) {
                    if (drawingBackground.Contains(PointToClient(Cursor.Position)) && Cursor != Cursors.Hand) {
                        SelectedIndex = curItem;
                        Cursor = Cursors.Hand;
                    }

                    if (curItem == SelectedIndex) {
                        using (LinearGradientBrush brush = new LinearGradientBrush(drawingBackground, Color.FromArgb(255, 252, 242), Color.FromArgb(255, 236, 181), LinearGradientMode.Vertical)) {
                            using (GraphicsPath path = CreateRoundedRectangle((int)drawingBackground.X, (int)drawingBackground.Y, (int)drawingBackground.Width, (int)drawingBackground.Height, BorderRadius)) {
                                graphics.FillPath(brush, path);
                            }
                        }

                        using (GraphicsPath path = CreateRoundedRectangle((int)drawingBackground.X, (int)drawingBackground.Y, (int)(drawingBackground.Width), (int)(drawingBackground.Height), BorderRadius)) {
                            graphics.DrawPath(new Pen(Color.FromArgb(229, 195, 101)), path);
                        }
                    }

                    graphics.DrawString(Items[curItem].ToString(), Font, SystemBrushes.WindowText, BorderRadius * 2, yPos + 2);
                }

                yPos += itemHeight;
                ++curItem;
            }
        }

        protected override void OnMouseMove(MouseEventArgs e) {
            Invalidate();
            base.OnMouseMove(e);
        }

        protected override void OnMouseLeave(EventArgs e) {
            Invalidate();
            base.OnMouseLeave(e);
        }

        protected virtual void OnSelectedIndexChanged(SelectedIndexChangedEventArgs e) {
            SelectedIndexChangedEventHandler handler = SelectedIndexChanged;
            if (handler != null) {
                handler(this, e);
            }
        }

        protected override void OnMouseClick(MouseEventArgs e) {
            if (e.Button == MouseButtons.Left && SelectedIndex > -1) {
                OnSelectedIndexChanged(new SelectedIndexChangedEventArgs(SelectedIndex));
            }

            base.OnMouseClick(e);
        }

        public ProjectListBox() {
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            BackColor = Color.Transparent;
            Font = new Font("Segoe UI", 10f);
        }
    }
}
